package
{
	import flash.media.SoundTransform;
	import com.bit101.components.Knob;
	import com.bit101.components.TextArea;
	import com.bit101.components.FPSMeter;
	import com.bit101.components.Label;
	import com.bit101.components.RadioButton;
	import flash.utils.setTimeout;
	import org.xiph.vorbis.VorbisSound;

	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	 * Basic demonstration of playing multiple files
	 *  
	 * @author Eugene Zatepyakin
	 */

	[SWF(width='300',height='250',frameRate='33',backgroundColor='0xDDDDDD')]

	public final class MultipleFilesTest extends Sprite
	{

		private const loops1:Vector.<String> = Vector.<String>(
																[
																'http://www.inspirit.ru/exchange/florbis/01_01_120_10_bass.ogg',
																'http://www.inspirit.ru/exchange/florbis/01_02_120_10_beat.ogg',
																'http://www.inspirit.ru/exchange/florbis/01_03_120_10_melody.ogg',
																'http://www.inspirit.ru/exchange/florbis/01_04_120_10_atmo.ogg'
																]);
		private const loops2:Vector.<String> = Vector.<String>(
																[
																'http://www.inspirit.ru/exchange/florbis/03_01_128_12_beat.ogg',
																'http://www.inspirit.ru/exchange/florbis/03_02_128_12_synth.ogg'
																]);
		private const loops3:Vector.<String> = Vector.<String>(
																[
																'http://www.inspirit.ru/exchange/florbis/04_01_128_2_lead.ogg',
																'http://www.inspirit.ru/exchange/florbis/04_02_128_2_beat.ogg'
																]);

		public const vfiles:Vector.<VorbisSound> = Vector.<VorbisSound>(
																	[
																	new VorbisSound(),
																	new VorbisSound(),
																	new VorbisSound(),
																	new VorbisSound()
																	]);

		public const loopSets:Vector.<Vector.<String>> = Vector.<Vector.<String>>(
																		[
																		loops1,
																		loops2,
																		loops3
																		]);
		public var currentSet:int = -1;
		public var selectedSet:Vector.<String>;

		public var rad0:RadioButton;
		public var rad1:RadioButton;
		public var rad2:RadioButton;
		public var inf:TextArea;

		public var knob0:Knob;
		public var knob1:Knob;
		public var knob2:Knob;
		public var knob3:Knob;

		public var loadedFiles:int;

		public function MultipleFilesTest()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.frameRate = 999.9;

			var lab:Label = new Label(this, 10, 10, 'SELECT LOOPS SET:');
			var fps:FPSMeter = new FPSMeter(this, 300-55, 250-20, 'FPS: ');

			inf = new TextArea(this, 10, 110, '');
			inf.height = 133;
			inf.width = 220;

			knob0 = new Knob(this, 10, 30, '', onKnobVolume);
			knob1 = new Knob(this, 70, 30, '', onKnobVolume);
			knob2 = new Knob(this, 130, 30, '', onKnobVolume);
			knob3 = new Knob(this, 190, 30, '', onKnobVolume);

			rad0 = new RadioButton(this, 10, 30, '4 SAMPLES', true, onPoolSelect);
			rad1 = new RadioButton(this, 90, 30, '2 SAMPLES', false, onPoolSelect);
			rad2 = new RadioButton(this, 170, 30, '2 SAMPLES', false, onPoolSelect);

			switchLoopsSet(0);
		}

		public function onPoolSelect(e:Event):void
		{
			var rad:RadioButton = RadioButton(e.currentTarget);
			switch(rad)
			{
				case rad0:
					switchLoopsSet(0);
					break;
				case rad1:
					switchLoopsSet(1);
					break;
				case rad2:
					switchLoopsSet(2);
					break;
			}
		}

		public function onKnobVolume(e:Event):void
		{
			var knob:Knob = Knob(e.currentTarget);
			switch(knob)
			{
				case knob0:
					setVorbisVolume(vfiles[0], knob.value / 100);
					break;
				case knob1:
					setVorbisVolume(vfiles[1], knob.value / 100);
					break;
				case knob2:
					setVorbisVolume(vfiles[2], knob.value / 100);
					break;
				case knob3:
					setVorbisVolume(vfiles[3], knob.value / 100);
					break;
			}
		}

		public function switchLoopsSet(id:int = 0):void
		{
			var i:int;
			var n:int;

			if(currentSet > -1)
			{
				n = selectedSet.length;
				for(i = 0; i < n; ++i)
				{
					vfiles[i].close();
					vfiles[i].removeEventListener( Event.COMPLETE, complete );
					//
					setVorbisVolume(vfiles[i], 1.0);
				}
			}

			selectedSet = loopSets[id];
			currentSet = id;
			n = selectedSet.length;
			loadedFiles = 0;

			inf.text = '';
			knob0.value = knob1.value = knob2.value = knob3.value = 100;

			for(i = 0; i < n; ++i)
			{
				playFile(vfiles[i], selectedSet[i], true);
			}
		}

		private function playFile(vorbis:VorbisSound, path:String, loop:Boolean = false):void
		{
			vorbis.addEventListener( Event.COMPLETE, complete );

			vorbis.load( new URLRequest( path ) );
			vorbis.loop = loop;
			//vorbis.play();
		}
		
		private function setVorbisVolume(vorbis:VorbisSound, vol:Number = 1.0):void
		{
			var st:SoundTransform = vorbis.soundChannel.soundTransform;
			st.volume = vol;
			vorbis.soundChannel.soundTransform = st;
		}

		//-- VORBIS FULLY LOADED
		private function complete( event:Event ):void
		{
			var vorbis:VorbisSound = VorbisSound(event.currentTarget);

			inf.text += '<<< VORBIS INFO >>>\n';
			inf.text += vorbis.info.debug() + '\n';
			inf.text += 'duration: ' + vorbis.duration + '\n';
			inf.text += '<<< ----------- >>>\n';

			checkToStart();
		}

		private function checkToStart():void
		{
			var n:int = selectedSet.length;

			if(++loadedFiles == n)
			{
				for(var i:int = 0; i < n; ++i)
				{
					vfiles[i].play();
				}
			}
		}

	}
}
